#[deriving(Clone)]
pub struct Layout {
lib: Option<Path>,
- bins: Vec<Path>
+ bins: Vec<Path>,
+ examples: Vec<Path>,
}
impl Layout {
pub fn project_layout(root: &Path) -> Layout {
let mut lib = None;
let mut bins = vec!();
+ let mut examples = vec!();
if root.join("src/lib.rs").exists() {
lib = Some(root.join("src/lib.rs"));
.map(|mut i| i.collect())
.map(|found| bins.push_all_move(found));
+ let _ = fs::readdir(&root.join("examples"))
+ .map(|v| v.move_iter())
+ .map(|i| i.filter(|f| f.extension_str() == Some("rs")))
+ .map(|mut i| i.collect())
+ .map(|found| examples.push_all_move(found));
+
Layout {
lib: lib,
- bins: bins
+ bins: bins,
+ examples: examples,
}
}
type TomlLibTarget = TomlTarget;
type TomlBinTarget = TomlTarget;
+type TomlExampleTarget = TomlTarget;
/*
* TODO: Make all struct fields private
project: Option<Box<TomlProject>>,
lib: Option<Vec<TomlLibTarget>>,
bin: Option<Vec<TomlBinTarget>>,
+ example: Option<Vec<TomlExampleTarget>>,
dependencies: Option<HashMap<String, TomlDependency>>,
dev_dependencies: Option<HashMap<String, TomlDependency>>
}
}).collect())
}
+fn inferred_example_targets(layout: &Layout) -> Option<Vec<TomlTarget>> {
+ Some(layout.examples.iter().filter_map(|ex| {
+ let name = ex.filestem_str().map(|f| f.to_string());
+
+ name.map(|name| {
+ TomlTarget {
+ name: name,
+ crate_type: None,
+ path: Some(ex.display().to_string()),
+ test: None,
+ plugin: None,
+ }
+ })
+ }).collect())
+}
+
impl TomlManifest {
pub fn to_manifest(&self, source_id: &SourceId, layout: &Layout)
-> CargoResult<(Manifest, Vec<Path>)>
}).collect())
};
+ let examples = if self.example.is_none() || self.example.get_ref().is_empty() {
+ inferred_example_targets(layout)
+ } else {
+ Some(self.example.get_ref().iter().map(|t| {
+ t.clone()
+ }).collect())
+ };
+
// Get targets
let targets = normalize(lib.as_ref().map(|l| l.as_slice()),
bins.as_ref().map(|b| b.as_slice()),
+ examples.as_ref().map(|e| e.as_slice()),
&metadata);
if targets.is_empty() {
fn normalize(lib: Option<&[TomlLibTarget]>,
bin: Option<&[TomlBinTarget]>,
+ example: Option<&[TomlExampleTarget]>,
metadata: &Metadata)
-> Vec<Target>
{
- log!(4, "normalizing toml targets; lib={}; bin={}", lib, bin);
+ log!(4, "normalizing toml targets; lib={}; bin={}; example={}", lib, bin, example);
enum TestDep { Needed, NotNeeded }
}
}
+ fn example_targets(dst: &mut Vec<Target>, examples: &[TomlExampleTarget],
+ default: |&TomlExampleTarget| -> String) {
+ for ex in examples.iter() {
+ let path = ex.path.clone().unwrap_or_else(|| default(ex));
+
+ let profile = &Profile::default_test().test(false);
+ {
+ dst.push(Target::example_target(ex.name.as_slice(),
+ &Path::new(path.as_slice()),
+ profile));
+ }
+ }
+ }
+
let mut ret = Vec::new();
match (lib, bin) {
(None, None) => ()
}
+
+ match example {
+ Some(ref examples) => {
+ example_targets(&mut ret, examples.as_slice(),
+ |ex| format!("examples/{}.rs", ex.name));
+ },
+ None => ()
+ }
+
ret
}
hash1 = hash1,
hash2 = hash2).as_slice());
})
+
+test!(explicit_examples {
+ let mut p = project("world");
+ p = p.file("Cargo.toml", r#"
+ [package]
+ name = "world"
+ version = "1.0.0"
+ authors = []
+
+ [[lib]]
+ name = "world"
+ path = "src/lib.rs"
+
+ [[example]]
+ name = "hello"
+ path = "examples/ex-hello.rs"
+
+ [[example]]
+ name = "goodbye"
+ path = "examples/ex-goodbye.rs"
+ "#)
+ .file("src/lib.rs", r#"
+ pub fn get_hello() -> &'static str { "Hello" }
+ pub fn get_goodbye() -> &'static str { "Goodbye" }
+ pub fn get_world() -> &'static str { "World" }
+ "#)
+ .file("examples/ex-hello.rs", r#"
+ extern crate world;
+ fn main() { println!("{}, {}!", world::get_hello(), world::get_world()); }
+ "#)
+ .file("examples/ex-goodbye.rs", r#"
+ extern crate world;
+ fn main() { println!("{}, {}!", world::get_goodbye(), world::get_world()); }
+ "#);
+
+ assert_that(p.cargo_process("cargo-test"), execs());
+ assert_that(process(p.bin("test/hello")), execs().with_stdout("Hello, World!\n"));
+ assert_that(process(p.bin("test/goodbye")), execs().with_stdout("Goodbye, World!\n"));
+})
+
+test!(implicit_examples {
+ let mut p = project("world");
+ p = p.file("Cargo.toml", r#"
+ [package]
+ name = "world"
+ version = "1.0.0"
+ authors = []
+ "#)
+ .file("src/lib.rs", r#"
+ pub fn get_hello() -> &'static str { "Hello" }
+ pub fn get_goodbye() -> &'static str { "Goodbye" }
+ pub fn get_world() -> &'static str { "World" }
+ "#)
+ .file("examples/hello.rs", r#"
+ extern crate world;
+ fn main() { println!("{}, {}!", world::get_hello(), world::get_world()); }
+ "#)
+ .file("examples/goodbye.rs", r#"
+ extern crate world;
+ fn main() { println!("{}, {}!", world::get_goodbye(), world::get_world()); }
+ "#);
+
+ assert_that(p.cargo_process("cargo-test"), execs());
+ assert_that(process(p.bin("test/hello")), execs().with_stdout("Hello, World!\n"));
+ assert_that(process(p.bin("test/goodbye")), execs().with_stdout("Goodbye, World!\n"));
+})